package com.smbms.tool;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.smbms.entity.User;

public class ExcelUtil {
	public String writerExcel(List<User> userList) throws FileNotFoundException, IOException {
		//创建Excel工作空间
    	HSSFWorkbook workbook = new HSSFWorkbook();
    	
    	//创建一页
    	HSSFSheet sheet = workbook.createSheet();
    	
    	//获取该页的第一行
    	HSSFRow row = sheet.createRow(0);
    	
    	//获取该行的第一格
    	HSSFCell cell = row.createCell(0);
    	cell.setCellValue("用户编码");
    	//获取该行的第二格
    	cell = row.createCell(1);
    	cell.setCellValue("用户名称");
    	cell = row.createCell(2);
    	cell.setCellValue("性别");
    	cell = row.createCell(3);
    	cell.setCellValue("年龄");
    	cell = row.createCell(4);
    	cell.setCellValue("电话");
    	cell = row.createCell(5);
    	cell.setCellValue("用户角色");
    	
    	String path = "D:/用户列表.xls";
    	FileOutputStream outputStream = new FileOutputStream(path);
    	
    	for (int i = 0; i < userList.size(); i++) {
    		//从第二行开始循环
    		row = sheet.createRow(i+1);
        	
        	//获取该行的第一格
        	cell = row.createCell(0);
        	cell.setCellValue(userList.get(i).getUserCode());
        	//获取该行的第二格
        	cell = row.createCell(1);
        	cell.setCellValue(userList.get(i).getUserName());
        	cell = row.createCell(2);
        	if(userList.get(i).getGender()==1){
        		cell.setCellValue("男");
        	}else{
        		cell.setCellValue("女");
        	}
        	cell = row.createCell(3);
        	cell.setCellValue(userList.get(i).getAge());
        	cell = row.createCell(4);
        	cell.setCellValue(userList.get(i).getPhone());
        	cell = row.createCell(5);
        	cell.setCellValue(userList.get(i).getUserRoleName());
		}
    	
    	workbook.write(outputStream);
    	outputStream.close();
    	return path;
	}
	
}